package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.consts.MallConst;
import com.mall.form.OrderForm;
import com.mall.pojo.User;
import com.mall.service.IOrderService;
import com.mall.vo.OrderVo;
import com.mall.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: 黄建
 * @Date: 2020/1/26 23:48
 */
@RestController
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/orders")
    public ResponseVo<OrderVo> create(HttpSession session,@RequestBody OrderForm orderForm){
        User user = (User)session.getAttribute(MallConst.CURRENT_USER);
        return orderService.create(user.getId(),orderForm.getShippingId());
    }

    @GetMapping("/orders")
    public ResponseVo<PageInfo<OrderVo>> list(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                              HttpSession session){
        User user = (User)session.getAttribute(MallConst.CURRENT_USER);
        return orderService.list(user.getId(),pageNum,pageSize);
    }

    @GetMapping("/orders/{orderNo}")
    public ResponseVo<OrderVo> detail(@PathVariable Long orderNo,
                                      HttpSession session){
        User user = (User)session.getAttribute(MallConst.CURRENT_USER);
        return orderService.detail(user.getId(),orderNo);
    }

    @PutMapping("/orders/{orderNo}")
    public ResponseVo cancelOrder(@PathVariable Long orderNo,
                                      HttpSession session){
        User user = (User)session.getAttribute(MallConst.CURRENT_USER);
        return orderService.cancelOrder(user.getId(),orderNo);
    }

}
